<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
    $orderby='star desc, id desc';
	$total='';
	$return['list']=pdo_getslice('jy_chengedu_jigou',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		$v['url']=custom_url('jy_chengedu/jigou//id='.$v['id'],true);
		$v['member']=cfc('core')->member_show($v['mid']);
		$v['catename']=pdo_getcolumn('jy_chengedu_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
	}
	$return['status']=array('0'=>array('id'=>0,'title'=>'待入驻'),'1'=>array('id'=>1,'title'=>'入驻中'),'2'=>array('id'=>2,'title'=>'暂停中'));
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'机构管理'),
			array('op'=>'edit','title'=>'添加机构'),
		),
		'list'=>array('data'=>'list','tip'=>'','list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('title'=>'机构信息_Merch','type'=>'img_string','img'=>'logo',
			'data'=>array(
				array('title'=>'名称：','name'=>'title'),
				array('title'=>'项目：','name'=>'salecate')
				)
			),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('title'=>'联系人_Merch','type'=>'img_string',
			'data'=>array(
				array('title'=>'联系人：','name'=>'realname'),
				array('title'=>'电话：','name'=>'mobile')
				)
			),
			array('name'=>'catename','title'=>'所属分类','type'=>'string'),
			array('name'=>'url','title'=>'入口','type'=>'ewm'),	
			array('name'=>'jointime','title'=>'入驻时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),						
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'机构名称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status2']=array('1'=>'入驻中','2'=>'暂停中');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_chengedu_jigou',array('id'=>$id));
		$item['lat_lng']=json_decode($item['lat_lng']);
		$item['onlinetime']=json_decode($item['onlinetime'],true);
	}else{
		$item=array();
	}
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array();
	$choufee_set=$item['choufee_set']=$item['choufee_set']?json_decode($item['choufee_set'],true):array('commission_type'=>'0','shopcommission_type'=>'0');
	if(!is_array($choufee_set)){
		$choufee_set=array('commission_type'=>'0','shopcommission_type'=>'0');
	}
	$choufee_set['rate']=$choufee_set['rate']?:0;	
	$item=array_merge($item,$choufee_set);
	$item['upass']='';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'机构设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'机构管理'),
			array('op'=>'edit','title'=>'添加机构'),
		),
		'edit'=>array(
			array('name'=>'title','must'=>true,'title'=>'机构名称','type'=>'string'),
			array('name'=>'logo','title'=>'机构logo','type'=>'image'),
			array('type'=>'image','title'=>'顶部图片','name'=>'topimg'),
			array('name'=>'star','title'=>'评星','type'=>'unitstring','unit'=>'星'),
			array('name'=>'cateid','must'=>true,'title'=>'机构分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'uname','must'=>true,'title'=>'账户名','type'=>'string'),
			array('name'=>'upass','title'=>'账户密码','type'=>'string'),
			array('name'=>'mid','title'=>'选择管理员','type'=>'choose','action'=>'get_member'),					
			array('name'=>'desc','title'=>'机构简介','type'=>'text'),
			array('type'=>'text','title'=>'机构公告','name'=>'remark'),
			array('name'=>'pcd','title'=>'机构区域','type'=>'pcd'),
			array('name'=>'address','title'=>'机构地址','type'=>'string'),	
			array('name'=>'lat_lng','must'=>true,'title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'tel','title'=>'机构电话','type'=>'string'),
			array('name'=>'isrecommand','title'=>'是否推荐','type'=>'radio','options'=>'status2'),	
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			//array('name'=>'groupid','title'=>'机构组','type'=>'select','options'=>'group','option_title'=>'groupname'),	
			array('name'=>'codeimg','title'=>'营业执照/许可证','type'=>'images'),
			array('name'=>'content','title'=>'机构详情','type'=>'richtext'),
		)
	);
	$return['cate']=pdo_getall('jy_chengedu_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['status']=array('0'=>'待审核','1'=>'入驻中','2'=>'暂停中');
	$return['status2']=array('1'=>'是','0'=>'否');
	$return['commission_types']=$this->commission_types;
	unset($return['commission_types'][3]);
	$return['dish_feetypes']=$this->dish_feetypes;
	$return['shop_feetypes']=$this->shop_feetypes;
	$return['no_feetypes']=$this->no_feetypes;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['title'] ||  !$post['uname']){
		exi('机构名称和账户必填','error');
	}
	if(!$post['cateid']){
		exi('机构分类必选！','error');
	}
	if(!$post['lat_lng']){
		exi('请填写坐标！','error');
	}
	if($post['star'] >5){
		exi('评星不能大于5！','error');
	}
	$infomid=pdo_getcolumn('jy_chengedu_jigou',array('mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']),'mid');
	$uname=pdo_get('jy_chengedu_jigou',array('uname'=>$post['uname']));
	$data =array(
		'uname'=>$post['uname'],
		'upass'=>base64_encode($post['upass']),
		'mobile'=>$post['mobile'],
		'realname'=>$post['realname'],
		'title'=>$post['title'],
		'offline'=>intval($post['offline']),
		'onlinetime'=>ijson_encode($post['onlinetime']),
		'logo'=>$post['logo'],
		'topimg'=>$post['topimg'],
		'desc'=>$post['desc'],
		'remark'=>$post['remark'],
		'salecate'=>$post['salecate'],
		'status'=>$post['status'],
		'address'=>$post['address'],
		'lat_lng'=>ijson_encode($post['lat_lng']),
		'mid'=>$post['mid'],
		'cateid'=>intval($post['cateid']),
		'star'=>intval($post['star']),
		'tel'=>$post['tel'],
		'groupid'=>$post['groupid'],
		'content'=>trim($post['content']),
		'codeimg'=>ijson_encode($post['codeimg']),
		'pcd'=>ijson_encode($post['pcd']),
	);
	if($post['id']){
		if(!$post['upass']){
			unset($data['upass']);
		}
		if($infomid && $infomid!=$post['mid']){
			//exi('一个管理员只能绑定一个账号','error');
		}
		if($uname && $uname['id']!=$post['id']){
			exi('登陆账户名不可以重复','error');	
		}
		pdo_update('jy_chengedu_jigou',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		if($uname){
			exi('登陆账户名不可以重复','error');	
		}
		if($infomid){
			//exi('一个管理员只能绑定一个账号','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['jointime']=time();
		pdo_insert('jy_chengedu_jigou',$data);
		$jigouid=pdo_insertid();
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($post['upass']));
		$uid=model('core/member')->register($member);
		pdo_update('jy_chengedu_jigou',array('mid'=>$uid),array('id'=>$jigouid));
		exi('添加成功','','list');
	}
	
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_chengedu_jigou',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_chengedu_jigou',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_chengedu_jigou',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
?>